package controller

import (
	"danmu/common"
	"danmu/dto"
	"danmu/model"
	"danmu/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

//关注
func Follow(ctx *gin.Context){
	DB :=common.GetDB()
	//获取参数
	var requestVideo = model.Relations{}
	ctx.Bind(&requestVideo)
	//需要关注的人的id和自己的id
	fid := requestVideo.ID
	uid,_ :=ctx.Get("id")
	//判断关注的是否为自己
	if fid == uid{
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"不能关注自己")
		return
	}
	//验证需要关注的人是否存在
	var user = model.User{}
	DB.First(&user, fid)
	if user.ID == 0 {
		response.Fail(ctx,nil,"用户不存在")
		return
	}
	//验证是否已经关注
	if IsFollowing(uid.(uint),fid){
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"已经关注")
		return
	}
	newRelations := model.Relations{
		Uid:uid.(uint),
		BuddyId:fid,
	}
	DB.Create(&newRelations)
	response.Success(ctx,nil,"关注成功")
}

//取消关注
func Unsubscribe (ctx *gin.Context){
	DB :=common.GetDB()
	//获取参数
	var requestVideo = model.Relations{}
	ctx.Bind(&requestVideo)
	//需要关注的人的id和自己的id
	fid := requestVideo.ID
	uid,_ :=ctx.Get("id")
	//验证关注是否存在
	if !IsFollowing(uid.(uint),fid){
		response.Response(ctx,http.StatusUnprocessableEntity,422,nil,"没有关注")
		return
	}
	//删除
	var relations = model.Relations{}
	DB.Where("uid = ? AND buddy_id = ?", uid, fid).Delete(&relations)
	response.Success(ctx,nil,"取消关注")
}

//获取关注列表
func GetFollowing(ctx *gin.Context)  {
	DB :=common.GetDB()
	var following []model.Relations
	var count int
	uid := ctx.DefaultQuery("uid", "0")
	if uid == "0" {
		response.Fail(ctx,nil,"用户不存在")
		return
	}
	DB.Where("uid = ?", uid).Find(&following).Count(&count)
	if count == 0{
		response.Success(ctx,nil,"还没有关注任何人")
		return
	}
	response.Success(ctx,gin.H{"count":count,"data":dto.ToFollowingDto(following,count)},"获取成功")
}

//粉丝列表
func GetFollower(ctx *gin.Context)  {
	DB :=common.GetDB()
	var follower []model.Relations
	var count int
	uid := ctx.DefaultQuery("uid", "0")
	if uid == "0" {
		response.Fail(ctx,nil,"用户不存在")
		return
	}
	DB.Where("buddy_id = ?", uid).Find(&follower).Count(&count)
	if count == 0{
		response.Success(ctx,nil,"没有粉丝")
		return
	}
	response.Success(ctx,gin.H{"count":count,"data":dto.ToFollowerDto(follower,count)},"获取成功")
}

//获取关注数和粉丝数
func GetFollowCount(ctx *gin.Context)  {
	DB :=common.GetDB()
	var relations []model.Relations
	var followerCount int
	var followingCount int
	uid := ctx.DefaultQuery("uid", "0")
	if uid == "0" {
		response.Fail(ctx,nil,"用户不存在")
		return
	}
	DB.Where("buddy_id = ?", uid).Find(&relations).Count(&followerCount)
	DB.Where("uid = ?", uid).Find(&relations).Count(&followingCount)
	response.Success(ctx,gin.H{"follower_count":followerCount,"following_count":followingCount},"获取成功")
}

func IsFollow(ctx *gin.Context)  {
	uid := ctx.DefaultQuery("uid", "0")
	if uid == "0" {
		response.Fail(ctx,nil,"用户不存在")
		return
	}
	fid := ctx.DefaultQuery("fid", "0")
	if fid == "0" {
		response.Fail(ctx,nil,"关注的人不存在")
		return
	}
	intUid,_ := strconv.Atoi(uid)
	uintUid := uint(intUid)
	intFid,_ := strconv.Atoi(fid)
	uintFid:= uint(intFid)
	//验证是否关注
	if IsFollowing(uintUid,uintFid){
		response.Success(ctx,nil,"已关注")
		return
	}
	response.Fail(ctx,nil,"没有关注")
}

//验证是否关注
func IsFollowing(uid uint,fid uint) bool {
	DB :=common.GetDB()
	var relations = model.Relations{}
	DB.Where("uid = ? AND buddy_id = ?", uid, fid).Find(&relations)
	if relations.ID == 0{
		return false
	}
	return  true
}